<?php

namespace App\Http\Controllers\Admin;

use App\Model\Cate;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;
use App\Http\Requests\CateRequest;

class CateController extends Controller
{
    /**
     * @return mixed
     * 分类列表
     */
    public function index(){
        $cateInfo = $this->getCateInfo();
        return view('/admin/cate/index',['cateInfo'=>$cateInfo]);
    }

    /**
     * @return mixed
     * 新增分类
     */
    public function add(){
        $cateInfo = $this->getCateInfo();
        return view('/admin/cate/add',['cateInfo'=>$cateInfo]);
    }

    /**
     * @param Request $request
     * 处理新增分类结果
     */
    public function addRes(CateRequest $request){
        $cate = new Cate();
        $data = $request->all();
        $cate->name = $data['name'];
        $cate->pid = $data['pid'];
        $cate->remark = $data['remark'];
        $res = $cate->save();
        if($res){
            return redirect('/admin/cate-list')->with('success','分类新增成功');
        }else{
            return back()->with('error','分类新增失败');
        }
    }

    /**
     * @param $id
     * @return mixed
     * 分类修改
     */
    public function update($id){
        $cate = new Cate();
        $cateData = $cate->find($id);
        $cateInfo = $this->getCateInfo();
        return view('/admin/cate/update',[
            'cateData' => $cateData,
            'cateInfo' => $cateInfo
        ]);
    }

    /**
     * @param Request $request
     * @return mixed
     * 分类修改结果
     */
    public function updateRes(Request $request){
        $data = $request->all();
        $cate = new Cate();
        $cateInfo = $cate->find($data['id']);
        $cateInfo->name = $data['name'];
        $cateInfo->pid = $data['pid'];
        $cateInfo->remark = $data['remark'];
        $res = $cateInfo->save();
        if($res){
            return redirect('/admin/cate-list')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    /**
     * @param $id
     * @return mixed
     * 获取分类名称
     */
    static public function getCate($id){
        $cate = new Cate();
        $cateName = $cate->where('id',$id)->first()->name;
        return $cateName;
    }

    /**
     * @param Request $request
     * 删除分类
     */
    public function delete(Request $request){
        $id = $request->input("cate_id");
        $cate = new Cate();
        $res = $this->deleteAllCate($id,$cate);
        if($res){
            echo 1;
        }else{
            echo 0;
        }
    }

    /**
     * @param $id
     * @param $obj
     * @return mixed
     * 递归删除当前分类所有子分类
     */
    private function deleteAllCate($id,$obj){
        $lowerId = $obj->where('pid',$id)->get();
        if(count($lowerId)){
            foreach ($lowerId as $k => $v){
                $this->deleteAllCate($v->id,$obj);
            }
        }
        $res = $obj->where('id',$id)->delete();
        return $res;
    }
}
